package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class classes {

    private int Cno;

    private String Cname;



    public classes(int cno,String cname){
        Cno = cno;
        Cname = cname;
    }

    public int getCno() {
        return Cno;
    }

    public void setCno(int cno) {
        Cno = cno;
    }

    public String getCname() {
        return Cname;
    }

    public void setCname(String cname) {
        Cname = cname;
    }



    public static HashMap<Integer,String> selectClasses(String studentAccount){

         Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        HashMap<Integer,String> hashMap=new HashMap<>();

        try {
            pstm=con.prepareStatement("SELECT c.* FROM classes c  WHERE c.`Cno` NOT IN(SELECT s.`Cno` FROM student s WHERE s.`Saccount`=?)");

            pstm.setString(1,studentAccount);

            rs=pstm.executeQuery();

             while(rs.next()){

                 int temp1=rs.getInt("Cno");

                String temp2=rs.getString("Cname");

/*                 if(temp2==null){
                    temp2="暂无数据";
                 }*/

                hashMap.put(temp1,temp2);

            }
        } catch (SQLException e) {
            e.printStackTrace();
            e.printStackTrace();
            System.out.println("失败-->classes.java-->selectClasses()");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

/*        for (Map.Entry<Integer,String> s:hashMap.entrySet()
             ) {
            System.out.println(s.getKey()+"--->"+s.getValue());
        }*/
        return hashMap;
    }

    public static HashMap<Integer,String> seeClasses(String studentAccount){

        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        HashMap<Integer,String> hashMap=new HashMap<>();

        try {
            pstm=con.prepareStatement("SELECT c.* FROM student s JOIN classes c ON s.`Saccount`=? AND s.`Cno`=c.`Cno`");

            pstm.setString(1,studentAccount);

            rs=pstm.executeQuery();

            while(rs.next()){

                int temp1=rs.getInt("Cno");

                String temp2=rs.getString("Cname");
/*                if(temp2==null){
                    temp2="暂无数据";
                }*/

                hashMap.put(temp1,temp2);

            }
        } catch (SQLException e) {
            e.printStackTrace();
            e.printStackTrace();
            System.out.println("失败-->classes.java-->seeClasses()");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

        return hashMap;
    }

    public static HashMap<users,classes> allClassesInfo(){
        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        HashMap<users,classes> hashMap=new HashMap<>();

        try {
            pstm=con.prepareStatement("SELECT c.*,u.Uname,u.Uaccount FROM classes c LEFT JOIN teacher t ON t.`Cno`=c.`Cno` LEFT JOIN users u ON u.`Uaccount`=t.`Taccount`");

            rs=pstm.executeQuery();


            while(rs.next()){

                int temp1=rs.getInt("Cno");

                String temp2=rs.getString("Cname");

                String temp3=rs.getString("Uname");

                String temp4=rs.getString("Uaccount");



                classes cl=new classes(temp1,temp2);

                users us=new users(temp4,temp3);

                hashMap.put(us,cl);

            }
        } catch (SQLException e) {
            e.printStackTrace();
            e.printStackTrace();
            System.out.println("失败-->classes.java-->allClassesInfo()");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }
        return hashMap;
    }

    public static boolean addClassInfo(int cno,String cname){
        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        int num=0;

        try {
            pstm=con.prepareStatement("INSERT INTO classes (Cno,Cname) VALUES (?,?)");

            pstm.setInt(1,cno);
            pstm.setString(2,cname);

            num=pstm.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("失败--->classes.java-->addClassInfo()");
            return false;
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

        if(num!=0){
            return true;
        }else{
            return false;
        }
    }

}
